package org.pageley.games.fleetcommand.domain.building;

import org.pageley.games.fleetcommand.domain.Resource;

public class Colony {
	private static final int FOOD_PER_HOUR = 10;
	private static final int HOUR = 3600;
	private static final int SECONDS_PER_FOOD = HOUR / FOOD_PER_HOUR;
	private static final int SECONDS_FOR_DOUBLE_POPULATION = HOUR * 200;
	private static final int MAX_FOOD = 1000;
	private static final double MAX_NUMBER_OF_BEINGS = 500;
	private double food;
	private double numberOfBeings;

	public void workFor(int seconds) {
		produceFood(seconds);
		eatFood(seconds);
		produceBeings(seconds);
	}

	private void produceFood(int seconds) {
		if(this.food < MAX_FOOD) {
			
			this.food += (double) seconds / SECONDS_PER_FOOD;
			
			if(this.food > MAX_FOOD) {
				this.food = MAX_FOOD;
			}
		}
	}

	private void eatFood(int seconds) {
		this.food -= this.numberOfBeings * seconds / (100 * HOUR);
	}
	
	private void produceBeings(int seconds) {
		if(this.numberOfBeings > 0 && this.numberOfBeings < MAX_NUMBER_OF_BEINGS) {
			this.numberOfBeings = this.numberOfBeings * Math.pow(2, (double) seconds / SECONDS_FOR_DOUBLE_POPULATION);
			if(this.numberOfBeings > MAX_NUMBER_OF_BEINGS) {
				this.numberOfBeings = MAX_NUMBER_OF_BEINGS;
			}
		}
	}

	public int getStock(Resource resource) {
		return resource == Resource.FOOD ? (int) this.food : 0;
	}

	public void addBeings(int amountOfBeings) {
		this.numberOfBeings = amountOfBeings;
	}

	public int getNumberOfBeings() {
		return (int) numberOfBeings;
	}
	
}
